#include <iostream>
#include <string>
#include <vector>
#include <functional>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <stdlib.h>
#include <cstring>

using namespace std;

typedef function<void(int)> func;
vector<func> funSet;
uint64_t count = 0;

void showCount(int signum = 0)
{
    cout << "进程捕捉到了一个信号" << signum << " Pid: " << getpid()
         << " final count: " << count << endl;
}
void showLog(int test = 0)
{
    cout << "日志功能" << endl;
}
void showUser(int test = 0)
{
    pid_t id = fork();
    if (id == 0)
    {
        // cout << "current user: "; 线程不安全
        char msg[32] = "current user: ";
        write(1, msg, strlen(msg));
        execl("/usr/bin/whoami", "whoami", (char *)NULL);
        exit(1);
    }
    wait(nullptr);
}
void Timed_Tasks(int test = 0)
{
    // flush_data
    // 断开连接 用户长时间不登录 再次登录要输入密码
}

// 定时器功能
void catchSig(int signum)
{
    for (auto &fun : funSet)
    {
        fun(signum);
    }
    alarm(1);
}
void addFun()
{
    funSet.push_back(showCount);
    funSet.push_back(showLog);
    funSet.push_back(showUser);
}
int main()
{
    addFun();

    signal(SIGALRM, catchSig);
    alarm(1); // 这个闹钟一旦触发就自动移除

    while (true)
        count++;
    return 0;
}
